/**
 * @see https://umijs.org/zh-CN/plugins/plugin-access
 * */

 import { getMatchMenuItem } from "./services/ant-design-pro/menu";
//  import { checkRole, matchPermission } from "./utils/permission";
import { matchPermission } from "./utils/permission";

export default function access(initialState) {
  console.log("initialState in access.js:",initialState);
  const { currentUser, menuData } = initialState || {};
  return {
    // canAdmin: currentUser && currentUser.access === 'admin',
    hasPerms: (perm) => {
      return matchPermission(currentUser?.permissions, perm);
    },
    hasNoPerms: (perm) => {
      return !matchPermission(currentUser?.permissions, perm);
    },
    // roleFiler: (route) => {
    //   return checkRole(currentUser?.roles, route.authority);
    // },
    authorize: (route) => {
      // console.log("authorize");
      // console.log("authorize menus:",menuData);
      if(menuData) {
        const items = getMatchMenuItem(route.path, menuData);
        if(!items || items.length === 0){
          return false;
        } else {
          return true;
        }
      }
      return true;
    }, // initialState 中包含了的路由才有权限访问
  };
}
